package at.chrenko.tu.vs.shared;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;

public class SimpleChannel implements Channel {

	protected BufferedReader in;
	protected PrintWriter out;

	public SimpleChannel(InputStream in, OutputStream out) {
		this.in = new BufferedReader(new InputStreamReader(in));
		this.out = new PrintWriter(out, true);
	}
	
	@Override
	public void send(String message) throws IOException {
		if(message == null)
			return;
		
		out.println(message);
	}
	
	@Override
	public void send(byte[] message) throws IOException{
		if(message == null)
			return;
		
		String output = new String(message, "UTF-8");
		out.println(output);
	}

	@Override
	public byte[] receive() throws IOException {
		String data = in.readLine();
		
		if(data == null)
			return null;
		
		return data.getBytes("UTF-8");
	}
	
	@Override
	public String receiveAsString() throws IOException {
		return in.readLine();
	}
}
